﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Entity;

namespace YiYiTong.Rocket.Api.Biz
{
    public class SysConfigService : ApiService
    {

        /// <summary>
        /// 获取转发配置
        /// </summary>
        /// <returns></returns>
        public void GetList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();

            List<SysConfig> list = SysConfigBiz.Instance.GetList(apiContext.CurrUserId);
            if (list != null && list.Count > 0)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { syslist = list });
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "转发配置为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }


        /// <summary>
        /// 保存转发配置
        /// </summary>
        /// <returns></returns>
        public void Write(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int config_type = dic.GetValue<int>("config_type", 1);
            string config_content = dic.GetValue<string>("config_content");
            if (config_type <= 0 || string.IsNullOrEmpty(config_content))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (!EnumHelper.IsDefined<ConfigType>(config_type))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "配置类型错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }
            string config_code = EnumHelper.ParseByNameOrValue<ConfigType>(config_type.ToString()).ToString();
            string config_name = EnumHelper.GetEnumItemDescription<ConfigType>(config_type);
            Tuple<bool, string> result = SysConfigBiz.Instance.Write(apiContext.CurrUserId, config_type, config_code, config_name, config_content);
            if (result.Item1)
                apiResponse.retCode = ResultEnum.Success;
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 版本检测
        /// </summary>
        public void CheckVersion(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int system_type = dic.GetValue<int>("system_type", 0);      //系统类型 1 android 2 ios
            int inno_version = dic.GetValue<int>("inno_version", 0);    //内部版本号
            if (inno_version <= 0 || system_type <= 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (!EnumHelper.IsDefined<SystemType>(system_type))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "系统类型错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = SysConfigBiz.Instance.CheckVersion(system_type, inno_version);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(result.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "无需更新版本";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        public void GetDownAddress(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int system_type = dic.GetValue<int>("system_type", 0);      //系统类型 1 android 2 ios
            if (system_type <= 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            if (!EnumHelper.IsDefined<SystemType>(system_type))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "系统类型错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = SysConfigBiz.Instance.GetDownAddress(system_type);
            if (result.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { url = result.Item2 });
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "未获得下载地址";
            }
            apiContext.apiHttpResp = apiResponse;
        }


        /// <summary>
        /// 获取轮播广告配置
        /// </summary>
        public void GetAd(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            int num = dic.GetValue<int>("num", 3);
            if (num < 0)
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "广告条数错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var list = SysConfigBiz.Instance.GetAd(num);
            if (list != null && list.Count > 0)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { adlist = list });
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "轮播广告为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 记录崩溃日志
        /// </summary>
        public void AddLog(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string content = dic.GetValue<string>("content");
            if (string.IsNullOrEmpty(content))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "日志内容为空";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            var result = SysConfigBiz.Instance.AddLog(content, apiContext.apiHttpReq.channel, apiContext.apiHttpReq.machineCode, Utils.GetClientIP());
            if (result.Item1)
                apiResponse.retCode = ResultEnum.Success;
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = result.Item2;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 获取银行列表
        /// </summary>
        /// <param name="apiContext"></param>
        public void GetBankList(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();

            var list = SysConfigBiz.Instance.GetBankList();
            if (list != null && list.Count > 0)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(new { list = list });
            }
            else
            {
                apiResponse.retCode = ResultEnum.NoData;
                apiResponse.message = "银行列表为空";
            }
            apiContext.apiHttpResp = apiResponse;
        }
    }
}